package strategy;

import strategy.ValidationStrategy;
import trans.ExpenseTransaction;
import trans.IncomeTransaction;
import trans.Transaction;

// 记账处理器
public class TransactionProcessor {
    private ValidationStrategy validationStrategy;

    public void setValidationStrategy(ValidationStrategy validationStrategy) {
        this.validationStrategy = validationStrategy;
    }

    public void processTransaction(Transaction transaction) {
        double amount = getAmount(transaction);
        if (validationStrategy.validate(amount)) {
//伪执行，通过观察者执行，保证数据一致性
            //            transaction.execute();
            System.out.println("记账成功执行！");
        } else {
            System.out.println("记账金额不符合要求，无法执行！");
        }
    }

    private double getAmount(Transaction transaction) {
        if (transaction instanceof ExpenseTransaction) {
            return ((ExpenseTransaction) transaction).getAmount();
        } else if (transaction instanceof IncomeTransaction) {
            return ((IncomeTransaction) transaction).getAmount();
        }
        return 0.0;
    }
}
